package com.stars.distributed.schedule.client.demo.remote;

import com.stars.distributed.schedule.client.DistributedTaskExecutionContent;
import com.stars.distributed.schedule.client.DistributedTaskExecutionResult;
import com.stars.distributed.schedule.client.RestDistributedTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Rest分布式任务demo
 *
 * @author guoguifang
 */
@RestController
@RequestMapping(value = "/api")
public class RestDistributedTaskDemo implements RestDistributedTask {

    private static final Logger logger = LoggerFactory.getLogger(RestDistributedTaskDemo.class);

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    @Override
    public DistributedTaskExecutionResult execute(HttpServletRequest request, @RequestBody DistributedTaskExecutionContent distributedTaskExecutionContent) {
        String name = request.getParameter("name");
        logger.info("接收到的name为: " + name);
        logger.info("接收到的分布式任务执行上下文为: " + distributedTaskExecutionContent);
        // 返回处理结果，这里设置为执行失败
        return distributedTaskExecutionContent.getSuccessResult();
    }
}
